import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { ContentType } from '../../common/enums';
import { User } from '../../users/entities/user.entity';
import { Attachment } from './attachment.entity';

@Entity('contents')
export class Content {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ length: 128 })
  title: string;

  @Column({
    name: 'content_type',
    type: 'enum',
    enum: ContentType,
    default: ContentType.TEXT,
  })
  contentType: ContentType;

  @Column({ type: 'text' })
  body: string;

  @Column({ name: 'recommend_reason', type: 'text', nullable: true })
  recommendReason: string;

  @Column({ name: 'template_id', nullable: true })
  templateId: string;

  @ManyToOne(() => Content, { nullable: true, createForeignKeyConstraints: false })
  @JoinColumn({ name: 'template_id' })
  template: Content;

  @Column({ name: 'created_by' })
  createdBy: string;

  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  @JoinColumn({ name: 'created_by' })
  creator: User;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  @Column({ name: 'is_approved', default: false })
  isApproved: boolean;

  @Column({ name: 'is_deleted', default: false })
  isDeleted: boolean;

  @OneToMany(() => Attachment, (attachment) => attachment.content, {
    cascade: true,
    createForeignKeyConstraints: false,
  })
  attachments: Attachment[];
}
